package com.briup.cms.web.controller;

import com.briup.cms.bean.User;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.UserService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import com.briup.cms.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "User")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService service;

    @ApiOperation(value = "注册用户",notes = "传递一个json对象的字符串")
    @PostMapping("/saveUser")
    public Result saveUser(@RequestBody User user){
        service.saveOrUpdateUser(user);
        return Result.success(user);
    }

    //根据角色name查找具体角色信息
    @ApiOperation(value = "查找用户",notes = "传递一个字符串的username")
    /*@ApiImplicitParams(
            @ApiImplicitParam(value = "username",name = "username")
    )*/
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/findUserByUsername")
    public Result findUserByUsername(String name,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        User user = service.findUserByUsername(name);
        if (user==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        return Result.success(user);
    }

    //查找所有用户信息
    @ApiOperation(value = "分页查找所有用户",notes = "传入第几页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/getUserAll")
    public Result getUserAll(Integer pageNum,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        Page<User> page = service.getAll(pageNum,20);
        return Result.success(page);
    }

    @ApiOperation(value = "通过Id删除用户",notes = "传入要删除对象的Id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @DeleteMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable Long id,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteUserById(id);
        return Result.success("单个删除成功");
    }

    @ApiOperation(value = "通过Id批量删除对象",notes = "传入要删除对象的Id的集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/deleteUserInBatch")
    public Result deleteUserInBatch(@RequestBody List<Long> ids,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteUserInBatch(ids);
        return Result.success("批量删除成功");
    }

    @ApiOperation(value = "登录",notes = "传入用户名和密码")
    @PostMapping("/login")
    public Result login(String username,String password){
        Map<String, Object> info = new HashMap<>();
        User user = service.login(username, password);
        info.put("username", user.getUsername());
        info.put("realname", user.getRealname());
        info.put("role",user.getRole().getId());
        String token = JwtUtil.sign(Long.toString(user.getId()), info);
        return Result.success("token: "+token);
    }

    @ApiOperation(value = "修改用户状态",notes = "传入id和状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/updateUserStatus")
    public Result updateUserStatus(Long id, String status, HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.updateUserStatus(id,status);
        return Result.success("操作成功");
    }

}
